Q: In a max-heap, element with the greatest key is always in the which node?
Solution: This is one of the property of max-heap that root node must have key greater than its children.
Q: What is the complexity of adding an element to the heap.
Solution: The total possible operation in re locating the new location to a new element will be equal to height of the heap.
Q: The worst case complexity of deleting any arbitrary node value element from heap is __________
Solution: The total possible operation in deleting the existing node and re locating new position to all its connected nodes will be equal to height of the heap.
Q: Heap can be used as ________________
Solution: The property of heap that the value of root must be either greater or less than both of its children makes it work like a priority queue.
Q: An array consists of n elements. We want to create a heap using the elements. The time complexity of building a heap will be in order of
Solution: The total time taken will be N times the complexity of adding a single element to the heap. And adding a single element takes logN time, so That is equal to N*logN.
Q: What is the space complexity of searching in a heap?
Solution: The space complexity of searching an element in heap is O (n). Heap consists of n elements and we need to compare every element. Here no addition or deletion of elements takes place. Hence space complexity is O (n).
Q: What is the best case complexity in building a heap?
Solution: The best case complexity occurs in bottom-up construction when we have a sortes array given.
Q: What is the location of a parent node for any arbitary node i?
Solution: For any node child nodes are located at either 2*i, 2*i +1 So the parent node could be found by taking the floor of the half of child node.
Q: State the complexity of algorithm given below. int function(vectorarr) int len=arr.length(); if(len==0) return; temp=arr[len-1]; arr.pop_back(); return temp;
Solution: Deletion in a min-heap is in O(1) time.
Q: For construction of a binary heap with property that parent node has value less than child node. In reference to that which line is incorrect. Line indexed from 1. 1. add(int k) 2. { 3. heap_size++; 4. int i = heap_size - 1; 5. harr[i] = k; 6. while (i != 0 && harr[parent(i)] < harr[i]) 7. { 8. swap(&harr[i], &harr[parent(i)]); 9. i = parent(i); 10. } 11. }
Solution: The condition under while condition is wrong for a (min) binary heap The correct condition should be while(i!=0 && harr[parent(i)] > harr[i]). Otherwise the constructed heap will be a max-binary heap.
You Have Score    | /10 |